package com.lyx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyx.entity.User;
import com.lyx.handler.BusinessException;
import com.lyx.response.Result;
import com.lyx.response.ResultCode;
import com.lyx.service.UserService;
import com.lyx.vo.UserVO;
import com.wf.captcha.utils.CaptchaUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author com.lyx
 * @since 2022-03-02
 */
@Api(tags = "用户管理器")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "查询所有用户")
    @GetMapping("/getUser")
    public Result getUserList(){
        List<User> users = userService.list();
        return  Result.ok().data("users",users);

    }

    @ApiOperation(value = "id用户列表",notes = "根据id查询用户列表")
    @GetMapping("/getUserById")
    public Result getUserById(Long id){
        User user =userService.getById(id);
        if (user==null){
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        return  Result.ok().data("user",user);
    }


    @ApiOperation(value = "分页用户列表",notes = "模糊查询用户列表加分页")
    @PostMapping("/findUserList")
    public Result findUserList(
     @RequestParam(value = "current",required = true,defaultValue = "1") Integer current,
     @RequestParam(value = "size",required = true,defaultValue = "7") Integer size,
     @RequestBody(required = false) UserVO userVO
    ){
        Page<User> page = new Page<>(current, size);
        Page<User> userPage = userService.findUserList(page, userVO);
        return  Result.ok().data("userPage",userPage);
    }

    @ApiOperation(value = "导出用户信息")
    @PostMapping(value = "/export")
    public void exportUserExcel(HttpServletResponse response, @RequestBody(required = false) UserVO userVO) throws IOException {
        Integer count = userService.getBaseMapper().selectCount(null);
        Page<User> page = new Page<>(1, count);

        Page<User> userList = userService.findUserList(page, userVO);
        List<User> records = userList.getRecords();
        userService.exportUserExcel(response,records);
    }


    /**
     * 用户登入
     *
     * @param username: 用户名
     * @param password: 密码
     * @return
     */
    @ApiOperation(value = "用户登入", notes = "接收参数用户名和密码,登入成功后,返回JWTToken")
    @PostMapping("/login")
    public Result login(
                        @RequestParam(value = "username",required = true) String username,
                        @RequestParam(value = "password",required = true ) String password,
                        @RequestParam(value = "verifyCode",required = true )  String verifyCode,
                              HttpServletRequest request) {
        System.out.println(verifyCode);
//        if (!CaptchaUtil.ver(verifyCode, request)) {
//            CaptchaUtil.clear(request);  // 清除session中的验证码
//            return Result.error().message("验证码错误");
//        }

        String token=userService.login(username,password);
     //   loginLogService.add(request);

        return Result.ok().data("token",token);
    }




}


